package Demo04.Demo04;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demo03_Collections {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("刘备",20,90));
        list.add(new Student("曹操",35,99));
        list.add(new Student("孙权",47,92));
        list.add(new Student("刘表",61,90));
        list.add(new Student("公孙瓒",65,100));

        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.scroce==o2.scroce){
                    return o1.age-o2.age;
                }
              return (int)(o2.scroce-o1.scroce);
            }
        });
        for (Student s : list){
            System.out.println(s);
        }
    }

}
class Student implements Comparable<Student>{
    String name;
    int age;
    double scroce;

    public Student() {
    }

    public Student(String name, int age, double scroce) {
        this.name = name;
        this.age = age;
        this.scroce = scroce;
    }

    @Override
    public int compareTo(Student o) {
        if (this.scroce==o.scroce){
            return this.age-o.age;
        }
        return (int)(o.scroce-this.scroce);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", scroce=" + scroce +
                '}';
    }
}
